/**
 * 用户模块：存token、个人信息
 */
// 导入登录的接口
import { loginApi, getUserInfoApi } from '@/api/user'
import { setToken, getToken, delToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  getters: {
  },
  mutations: {
    /**
     *
     * @param {*} state
     * @param {*} payload
     */
    setToken (state, payload) {
      state.token = payload
      setToken(payload)
    },
    delToken (state) {
      state.token = ''
      delToken()
    },
    setUserInfo (state, payload) {
      state.userInfo = payload
    }

  },
  actions: {
    async getTokenAction ({ commit }, payload) {
      const { data } = await loginApi(payload)
      console.log('token:', data)
      if (data.code === 0) {
        commit('setToken', data.token)
      }
      return data
    },
    async getUserAction ({ commit }) {
      const { data } = await getUserInfoApi()
      console.log('用户信息：', data)
      commit('setUserInfo', data.data)
    }
  }

}
